package jalus.service;

import jalus.dao.BlogLinkDAO;
import jalus.model.BlogLink;
import jalus.utils.PojoHandler;

import java.util.Collections;
import java.util.List;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheFactory;
import javax.cache.CacheManager;

public class BlogLinkService {
	private static BlogLinkService instance = new BlogLinkService();
	private BlogLinkDAO blogLinkDAO;
	private Cache cache;
	private BlogLinkService(){
		blogLinkDAO = new BlogLinkDAO();
		try {
			CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
			cache = cacheFactory.createCache(Collections.emptyMap());
		} catch (CacheException e) {
		}
	}
	public static BlogLinkService getInstance(){
		return instance;
	}
	public void deleteById(Long id) {
		blogLinkDAO.deleteById(id);
		cache.clear();
	}
	public void add(BlogLink blogLink) {
		blogLinkDAO.add(blogLink);
		cache.clear();
	}
	/**
	 * 获得所有链接
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<jalus.pojo.BlogLink> getAll() {
		String key = "getAll";
		if(!cache.containsKey(key)){
			cache.put(key, PojoHandler.copyMToPList(blogLinkDAO.getALL()));
		}
		return (List<jalus.pojo.BlogLink>) cache.get(key);
	}
}
